package com.doubi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.doubi.entity.Inform;
import com.doubi.entity.Releasetable;
import com.doubi.service.LiketableService;
import com.doubi.service.ReleasetableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@CrossOrigin("*")
@RestController
public class LiketableController {
    @Autowired
    private LiketableService liketableService;
    @Autowired
    private ReleasetableService releasetableService;
    /**
     * 添加喜欢
     */
    @PostMapping("addlike")
    public Object addlike(@RequestBody Releasetable releasetable) {
        Inform.Liketable liketable = new Inform.Liketable();
        liketable.setReleaseId(releasetable.getReleaseId());
        liketable.setUserId(releasetable.getUserId());
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String date = now.format(formatter);
        liketable.setLikeTime(date);
        QueryWrapper<Inform.Liketable> qw = new QueryWrapper<>(liketable);
        qw.eq("releaseId", releasetable.getReleaseId()).and(i->i.eq("userId", releasetable.getUserId()));
        Inform.Liketable one = liketableService.getOne(qw);
        if(one != null) {
            return "201";
        }
        return liketableService.save(liketable);
    }
    /**
     * 取消喜欢
     */
    @PostMapping("dellike")
    public Object dellike(@RequestBody Releasetable releasetable) {
        QueryWrapper<Inform.Liketable> wrapper = new QueryWrapper<>();
        wrapper.eq("releaseId", releasetable.getReleaseId());
        wrapper.eq("userId", releasetable.getUserId());
        return liketableService.remove(wrapper);
    }
    /**
     * 查询是否喜欢
     */
    @PostMapping("selectTrueLike")
    public Object selectUserInfo(@RequestBody Releasetable releasetable) {
        QueryWrapper<Inform.Liketable> qw = new QueryWrapper<>();
        qw.eq("releaseId", releasetable.getReleaseId()).and(i->i.eq("userId", releasetable.getUserId()));
        return liketableService.getOne(qw);
    }
    @GetMapping("selectTrueLike2")
    public Object selectUserInfo2(Integer releaseId,Integer userId) {
        QueryWrapper<Inform.Liketable> qw = new QueryWrapper<>();
        qw.eq("releaseId", releaseId).and(i->i.eq("userId", userId));
        return liketableService.getOne(qw);
    }
    /**
     * 单个视频查询所有喜欢
     */
    @PostMapping("selectlikeNum")
    public Object selectLikeNum(@RequestBody Releasetable releasetable) {
        return liketableService.releaseCountLike(releasetable.getReleaseId());
    }
    /**
     * 查询总共获赞数
     */
    @GetMapping("selectCount")
    public Object selectCount(Integer uid) {
        Integer num=0;
        //查询视频总数
        QueryWrapper<Releasetable> qw = new QueryWrapper<>();
        qw.eq("userId", uid);
        List<Releasetable> list = releasetableService.list(qw);
        if(list != null && list.size() > 0) {
            for (Releasetable releasetable : list) {
                num+=liketableService.releaseCountLike(releasetable.getReleaseId());
            }
        }
        return num.toString();
    }
}
